<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * bookStatitics controller
 * @author Phuc Nguyen - 09/03/2015
 * http://www.facebook.com/phucthuong
 * email : nguyenvanphuc0626@gmail.com
*/
class BookStatitics extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('bookStatitics');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('bookStatistic_model');
			$this->set_controller('bookStatitics');
			$this->set_model($this->bookStatistic_model);
		}
	}
        
        private function menuTop(){
            $this->load->model('stats_model');
            $user_info                          = $this->stats_model->get_user_info($this->stats_model->get_user_id());
            $this->view_data['role_by_group']   = $user_info['role_list'];
            $this->view_data['username']        = $user_info['username'];
            return $this->view_data;
        }
        
        public function lists(){
            $this->menuTop();
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $beginTime                      = ($this->input->post('beginTime'))?date('Y-m-d',strtotime($this->input->post('beginTime'))):'';
                $endTime                        = ($this->input->post('endTime'))?date('Y-m-d',strtotime($this->input->post('endTime'))):'';
                $this->view_data['lists']       = $this->bookStatistic_model->statiticUsers($beginTime,$endTime,array('156','175'));
                
               //export data to excel
               if($this->input->post('submit') == 'export'){
                    $list_user = $this->bookStatistic_model->getListUsers('id > 159 AND id < 175');
                    unset($list_user[""]);
                    $lists = array();
                    foreach($list_user as $key => $values){
                       $check=false;
                       foreach($this->view_data['lists'] as $vals){
                           if($vals->id == $key){
                                $username           = $vals->username;
                                $booking_hold       = $vals->booking_hold;
                                $booking_ok         = $vals->booking_ok;

                                $check = true;break;
                           }
                       }
                       
                       if($check == false){
                           $username        = $values;
                           $booking_hold    = 0;
                           $booking_ok      = 0;
                       }
                        $lists[] = array(
                                'username'          =>  $username,
                                'booking_hold'      =>  $booking_hold,
                                'booking_ok'        =>  $booking_ok,
                                'thoi gian'         =>  date('d-m-Y')
                        );
                    }

                    header("Content-Disposition: attachment; filename=\"danh-sach-booking-".date('d-m-Y').".xls\"");
                    header("Content-Type: application/vnd.ms-excel;");
                    header("Pragma: no-cache");
                    header("Expires: 0");
                    $out        = fopen("php://output", 'w');
                    echo "\nTen dang nhap\t Booking hold\t Booking da thanh toan\t Thoi gian\n";
                    foreach ($lists as $data)
                    {
                        fputcsv($out, $data,"\t");
                    }
                    fclose($out);
                    unset($this->view_data['lists']);
                    unset($lists);
                    unset($list_user);
                    die("");
               }
            } else {
                $this->view_data['lists']     = $this->bookStatistic_model->statiticUsers(date('Y-m-d'),date('Y-m-d'),array('156','175'));
            }
            $this->load->view('auth/bookStatitics/lists', $this->view_data);
        }
}